package main

import (
	"fmt"
	"net"
)

func mainsc() {
	// 创建拨号
	ip := "127.0.0.1"
	port := 8848
	address := fmt.Sprintf("%s:%d", ip, port)
	conn, err := net.Dial("tcp", address)
	if err != nil {
		fmt.Println("net.Dial err:", err)
		return
	}

	fmt.Println("client与server 连接建立成功")

	// 发送数据
	for i := 0; i < 10; i++ {
		sendData := []byte("helloworld")
		cnt, err := conn.Write(sendData)
		if err != nil {
			fmt.Println("conn.Write err", err)
			return
		}

		fmt.Println("Client ===> Server cnt", cnt, ", data: ", string(sendData))

		// 接收服务器数据
		buf := make([]byte, 1024)
		cnt, err = conn.Read(buf)
		if err != nil {
			fmt.Println("conn.Read err", err)
			return
		}
		fmt.Println("Client <=== Server cnt", cnt, ", data: ", string(buf[:cnt]))
	}
}
